<template>
	<view class="full-page theme-1 root-style">
		<!-- #ifdef MP -->
		<u-navbar is-fixed :background="{background: '#00000000'}" :border-bottom="false"></u-navbar>
		<!-- #endif -->
		<view>
			<user-header></user-header>
			<view class="stbar" v-if="ktluser.hasRole(2)">
				<view>累计上课:{{basedata.LT1Count+basedata.LT2Count}}</view>
				<view style="margin-left: 20rpx;">正课:{{basedata.LT1Count}}</view>
				<view style="margin-left: 20rpx;">复习课:{{basedata.LT2Count}}</view>
			</view>
			<view class="stbar" v-else>
				<view>学习单词:{{basedata.LS0Count+basedata.LS9Count}}个</view>
				<view style="margin-left: 20rpx;">熟词:{{basedata.LS0Count}}个</view>
				<view style="margin-left: 20rpx;">生词:{{basedata.LS9Count}}个</view>
			</view>
			<view style="margin:30rpx">
				<view v-if="ktluser.hasRole(2)" class="card" style="margin-bottom: 30rpx;padding: 7px 10px;">
					<view class="flex items-center vtitle" style="margin-bottom: 0;">
						<view style="flex: 1;">学生预约</view>
						<view style="flex: 1;margin-top: -1px;"><u-tag :text="yuList.length+'节课'" mode="dark" /></view>
						<view style="flex: 1;"><u-tag @click="goyy()" text="查看" mode="light" /></view>
					</view>
				</view>
				
				<view class="vtitle">今日课程 <text style="font-size: var(--text-14); color: #777; font-weight: normal;">({{todaylist.length}}节)</text> </view>
				<view class="vtitle" style="font-size: var(--text-16);">
					正式课({{todayLs1.length}})
				</view>
				<view class="card">
					<view class="card-item text-14" v-for="(item,index) in todayLs1" :key="index">
						<view style="flex:1;">{{item.Title}}</view> 
						<view style="flex:1;">{{dateFormat(item.StartTime,"yyyy-MM-dd HH:mm")}}</view>
						<view style="text-align: center;">
							<view>
								<u-tag :in-style="tagStyle" v-if="ktluser.hasRole(2)" @click="gotoless(item)" text="上课" mode="light" />
							</view>
							<view style="margin-top: 3px;">
								<u-tag :in-style="tagStyle" @click="enterroom(item)" text="入会" mode="light" />
							</view>
							<!-- <u-link :href="item.RoomLink" under-line>进入</u-link>  -->
						</view>
					</view>
					<u-empty icon-size="90" v-if="todayLs1.length == 0" text="暂 无" mode="list"></u-empty>
				</view>
				<view class="vtitle" style="margin-top: 20rpx; font-size: var(--text-16);">
					保温课({{todayLs2.length}})
				</view>
				<view class="card">
					<view class="card-item" v-for="(item,index) in todayLs2" :key="index">
						<view style="flex:1;">{{item.Title}}</view> 
						<view style="flex:1;">{{dateFormat(item.StartTime,"yyyy-MM-dd HH:mm")}}</view>
						<view style="width:100rpx; text-align: center;">
							<view>
								<u-tag v-if="ktluser.hasRole(2)" @click="gotorevless(item)" text="上课" mode="light" />
							</view>
							<view style="margin-top: 3px;">
								<u-tag @click="enterroom(item)" text="入会" mode="light" />
							</view>
							<!-- <u-link :href="item.RoomLink" under-line>进入</u-link>  -->
						</view>
					</view>
					<u-empty icon-size="90" v-if="todayLs2.length == 0" text="暂 无" mode="list"></u-empty>
				</view>
			</view>
			<view style="margin:30rpx">
				<view class="vtitle">待评价 <text style="font-size: var(--text-14); color: #777; font-weight: normal;">({{pjlist.length}}节)</text> </view>
				<view class="card">
					<view class="card-item text-14" v-for="(item,index) in pjlist" :key="index">
						<view style="flex:1;">{{item.Title}}</view>
						<view style="margin-right: 10px;">{{dateFormat(item.StartTime,"yyyy-MM-dd HH:mm")}}</view>
						<view><u-tag :in-style="tagStyle" @click="gotopj(item)" text="评价" mode="light" /></view>
					</view>
					<u-empty icon-size="90" v-if="pjlist.length == 0" text="暂 无" mode="list"></u-empty>
				</view>
			</view>
			<view style="margin:30rpx" v-if="!ktluser.hasRole(2)">
				<view class="vtitle">需复习 <text style="font-size: var(--text-14); color: #777; font-weight: normal;">({{reviewlist.length}}节)</text> </view>
				<view class="card">
					<view class="card-item text-14" v-for="(item,index) in reviewlist" :key="index">
						<view style="flex:1;">{{item.Title}}</view>
						<view style="width: 120rpx; text-align: center;">第{{item.ReviewTimer}}轮</view>
						<view style="width: 120rpx; text-align: center;">{{item.NeedReviewCount}}词</view>
						<view><u-tag :in-style="tagStyle" @click="goreview(item)" text="复习" mode="light" /></view>
					</view>
					<u-empty icon-size="90" v-if="reviewlist.length == 0" text="暂 无" mode="list"></u-empty>
				</view>
			</view>
		</view>
		<u-tabbar :list="tabbar" :height="tabbarConfig.height" :icon-size="tabbarConfig.iconSize"
			:text-size="tabbarConfig.textSize" hide-tab-bar></u-tabbar>
	</view>
</template>

<script setup>
	import { onMounted } from 'vue';
	import { onShow } from '@dcloudio/uni-app';
	import { ktlapi, ktluser, OT, dateFormat, TXRoom } from '@/utils/index.js';
	import { useTabbarStore } from '@/store/tabbar';
	import { useTabbarConfig } from '@/common/hooks/tabbar-config.js';
	const { tabbar, bus } = useTabbarStore();
	const { tabbarConfig } = useTabbarConfig();

	const basedata = ref({});
	const todaylist = ref([]);
	const pjlist = ref([]);
	const reviewlist = ref([]);
	const yuList = ref([]);

	const todayLs1 = computed(() => {
		return todaylist.value.filter(x => {
			return x.LessonType == 1
		});
	})

	const todayLs2 = computed(() => {
		return todaylist.value.filter(x => {
			return x.LessonType == 2
		});
	})
	const tagStyle = {
		padding: '5px 8px'
	};

	function enterroom(item) {
		console.log(item);
		TXRoom(item.RoomLink, item.RoomNo,item.RoomPwd);
	}

	function goyy() {
		uni.setStorageSync("status", 1);
		uni.switchTab({
			url: '/pages/tabbar/class/class',
		})
	}

	function loadStudent() {
		uni.showLoading({
			title: '加载中'
		});
		ktlapi({
			apiurl: OT.CommandSet,
			action: "Cus_HomeStudent",
			data: {
				"AccountId": ktluser.BaseInfo().AccountID,
			}
		}).then(res => {
			basedata.value = res.data.tables[0].table[0];
			todaylist.value = res.data.tables[1].table;
			pjlist.value = res.data.tables[2].table;
			reviewlist.value = res.data.tables[3].table;
			uni.hideLoading();
		});
	}

	function goreview(item) {
		uni.navigateTo({
			url: "/pages-review/review-test/index?uuid=" + item.LessonUUID
		})
	}

	function gotoless(item) {
		console.log(item);
		uni.navigateTo({
			url: '/pages-study/study/study?LessonId=' + item.UUID
		})
	}

	function gotorevless(item) {
		uni.setStorageSync("ReviewUUID", item.UUID);
		uni.setStorageSync("ReviewAccountID", item.StudentAccountID);
		uni.navigateTo({
			url: '/pages-review/review/review?ReviewUUID=' + item.UUID + '&ReviewAccountID=' + item
				.StudentAccountID
		})
	}

	function gotopj(item) {
		uni.navigateTo({
			url: `/pages-study/study/modules/step-final-result?LessonId=${item.UUID}&Type=view&isreview=1`
		})
	}

	function loadTeacher() {
		uni.showLoading({
			title: '加载中'
		});
		ktlapi({
			apiurl: OT.CommandSet,
			action: "Cus_HomeTeacher",
			data: {
				"AccountId": ktluser.BaseInfo().AccountID,
			}
		}).then(res => {
			basedata.value = res.data.tables[0].table[0];
			todaylist.value = res.data.tables[1].table;
			pjlist.value = res.data.tables[2].table;
			yuList.value = res.data.tables[3].table;
			uni.hideLoading();
		});
	}
	onShareAppMessage(() => {})
	onShow(() => {
		// #ifdef MP   
		wx.getSetting({
			withSubscriptions: true,
			success(res) {
				console.log("getSetting", res.subscriptionsSetting);
				if (!res.subscriptionsSetting.mainSwitch) {
					wx.requestSubscribeMessage({
						tmplIds: ['6pVNdHFwWXBg2O3r7wEmOWOK9JTUfAWwlftDrNYmFC4'],
						success(res) {
							console.log('消息订阅成功', res);
							wx.login({
								success(loginres) {
									console.log("MP登录成功", loginres);
									uni.showLoading({
										title: '订阅中'
									});
									ktlapi({
										apiurl: "MP/GetOpenId",
										data: {
											"Code": loginres.code,
											"LoginUid": ktluser.BaseInfo()
												.AccountID
										}
									}).then(res => {
										uni.hideLoading();
									});
								}
							});
						}
					})
				}
			}
		})

		// #endif

		if (ktluser.hasRole(2)) {
			loadTeacher();
		} else if (ktluser.hasRole([3, 4]) && !ktluser.hasRole(2)) {
			loadStudent();
		}
	})

	onMounted(() => {
		// console.log('home onMounted')
	})
</script>

<style lang="scss">
	.full-page {
		--card-margin: 30rpx;
		background-color: #f8f9fb !important;
	}

	.card {
		background-color: #fff;
		border-radius: 8px;
		padding: 4px 2px;
	}

	.card-item {
		padding: 9px 5px;
		line-height: 20px;
		display: flex;
		align-items: center;
		border-bottom: 1px solid #332C2B12;
	}
	
	.vtitle{
		font-size: var(--text-18);
		font-weight: bold;
		color: #332C2B;
		margin-bottom: 20rpx;
	}



	.stbar {
		    display: flex;
		    // background-image: url(https://nx5.abc5g.com/res/static/stbar.png);
		    background-size: 100% 100%;
		    background-repeat: no-repeat;
		    width: 90%;
		    margin: 0px auto;
		    font-size: var(--text-18);
			color: #ec6d25;
		    align-items: center; 
		    padding-left: 30rpx;
	}
</style>